﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Windows;
using System.Windows.Controls;
using System.Text.RegularExpressions;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Win32;

namespace 毕业设计__排课软件
{
    /// <summary>
    /// PK.xaml 的交互逻辑
    /// </summary>
    public partial class Pk
    {
        private readonly List<Lesson> _list = new List<Lesson>();
        List<TeachingProject> _classInfo = new List<TeachingProject>();
        readonly string _dbPath;
        int _i;

        public Pk()
        {
            InitializeComponent();

            #region 初始化课表
            _list.Add(new Lesson
            {
                lesson1 = "",
                lesson2 = "",
                lesson3 = "",
                lesson4 = "",
                lesson5 = "",
                week1 = "",
                week2 = "",
                week3 = "",
                week4 = "",
                week5 = "",
                didian1 = "",
                didian2 = "",
                didian3 = "",
                didian4 = "",
                didian5 = "",
                teacher1 = "",
                teacher2 = "",
                teacher3 = "",
                teacher4 = "",
                teacher5 = ""
            });
            _list.Add(new Lesson
            {
                lesson1 = "",
                lesson2 = "",
                lesson3 = "",
                lesson4 = "",
                lesson5 = "",
                week1 = "",
                week2 = "",
                week3 = "",
                week4 = "",
                week5 = "",
                didian1 = "",
                didian2 = "",
                didian3 = "",
                didian4 = "",
                didian5 = "",
                teacher1 = "",
                teacher2 = "",
                teacher3 = "",
                teacher4 = "",
                teacher5 = ""
            });
            _list.Add(new Lesson
            {
                lesson1 = "",
                lesson2 = "",
                lesson3 = "",
                lesson4 = "",
                lesson5 = "",
                week1 = "",
                week2 = "",
                week3 = "",
                week4 = "",
                week5 = "",
                didian1 = "",
                didian2 = "",
                didian3 = "",
                didian4 = "",
                didian5 = "",
                teacher1 = "",
                teacher2 = "",
                teacher3 = "",
                teacher4 = "",
                teacher5 = ""
            });
            _list.Add(new Lesson
            {
                lesson1 = "",
                lesson2 = "",
                lesson3 = "",
                lesson4 = "",
                lesson5 = "",
                week1 = "",
                week2 = "",
                week3 = "",
                week4 = "",
                week5 = "",
                didian1 = "",
                didian2 = "",
                didian3 = "",
                didian4 = "",
                didian5 = "",
                teacher1 = "",
                teacher2 = "",
                teacher3 = "",
                teacher4 = "",
                teacher5 = ""
            });
            _list.Add(new Lesson
            {
                lesson1 = "",
                lesson2 = "",
                lesson3 = "",
                lesson4 = "",
                lesson5 = "",
                week1 = "",
                week2 = "",
                week3 = "",
                week4 = "",
                week5 = "",
                didian1 = "",
                didian2 = "",
                didian3 = "",
                didian4 = "",
                didian5 = "",
                teacher1 = "",
                teacher2 = "",
                teacher3 = "",
                teacher4 = "",
                teacher5 = ""
            });
            #endregion

            var path0 = Environment.CurrentDirectory;
            var path1 = path0.Substring(0, path0.LastIndexOf("\\", StringComparison.Ordinal));
            var path2 = path1.Substring(0, path1.LastIndexOf("\\", StringComparison.Ordinal));
            _dbPath = path2 + "\\DB\\bs_pk.db";                                                  //sqlite数据库文件目录获取

            #region 数据库操作
            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();
                using (new SQLiteCommand(conn))
                {
                    const string sql = "select major from Class_Info GROUP BY major";         //查询数据库当中的专业信息
                    var command = new SQLiteCommand(sql, conn);
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        CbProfessional.Items.Add(reader["major"].ToString());                 //将查询到的专业信息显示在前台combox上
                    }
                    CbProfessional.Items.Add("全部");                                         //在combox中添加全部选项卡
                }

                using (new SQLiteCommand(conn))
                {
                    const string sql = "select Grade from Class_Info GROUP BY Grade";
                    var command = new SQLiteCommand(sql, conn);
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        CbClass.Items.Add(reader["Grade"].ToString());
                    }
                }

                using (new SQLiteCommand(conn))
                {
                    const string sql = "SELECT * FROM Lesson_Info";
                    var command = new SQLiteCommand(sql, conn);
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        var tp = new TeachingProject
                        {

                            CourseName = reader["Course_Name"].ToString(),
                            CourseId = reader["Course_ID"].ToString(),
                            TotalHours = reader["Total_Class"].ToString(),
                            /*WeekOfTeaching = reader["Teaching_Week"].ToString(),*/
                            UseComputerRoom = reader["jifang_Num"].ToString(),
                            UseClassroomHours = reader["jiaoshi_Num"].ToString(),
                            UsePcClassroom = reader["qita_Num"].ToString(),
                        };
                        _classInfo.Add(tp);
                    }
                    LbCourse.ItemsSource = _classInfo;
                }
            }
            #endregion
            LbClassInfo.SelectedIndex = 0;

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)                //初始化窗口中分页控件内的教室信息
        {
            #region 数据库操作初始化分页控件内的教室信息
            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();

                try
                {
                    using (new SQLiteCommand(conn))
                    {
                        string sql = "SELECT Classroom_Name FROM Classroom_Info where Classroom_Type ='多媒体'";
                        var command = new SQLiteCommand(sql, conn);                 //查询课程的详细信息
                        var reader = command.ExecuteReader();

                        while (reader.Read())
                        {
                            LbMulClassroom.Items.Add(reader["Classroom_Name"]);
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();

                try
                {
                    using (new SQLiteCommand(conn))
                    {
                        string sql = "SELECT Classroom_Name FROM Classroom_Info where Classroom_Type ='笔记本实验室'";
                        var command = new SQLiteCommand(sql, conn); //查询课程的详细信息
                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            LbLapClassroom.Items.Add(reader["Classroom_Name"]);
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();

                try
                {
                    using (new SQLiteCommand(conn))
                    {
                        string sql = "SELECT Classroom_Name FROM Classroom_Info where Classroom_Type ='机房'";
                        var command = new SQLiteCommand(sql, conn);                 //查询课程的详细信息
                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            LbMacClasseoom.Items.Add(reader["Classroom_Name"]);
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();

                try
                {
                    using (new SQLiteCommand(conn))
                    {
                        string sql = "SELECT Classroom_Name FROM Classroom_Info where Classroom_Type ='普通教室'";
                        var command = new SQLiteCommand(sql, conn);                 //查询课程的详细信息
                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            LbComClasseoom.Items.Add(reader["Classroom_Name"]);
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }
            #endregion
        }


        private void DG_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            if (_i == 0)
            {
                e.Row.Header = "    一节\n8:20-9:05\n    二节\n9:15-10:00";
                e.Row.MinHeight = 125;
                e.Row.MaxHeight = 125;
                _i++;
            }
            else if (_i == 1)
            {
                e.Row.Header = "    三节\n10:20-11:05\n    四节\n11:15-12:00";
                e.Row.MinHeight = 125;
                e.Row.MaxHeight = 125;
                _i++;
            }
            else if (_i == 2)
            {
                e.Row.Header = "    五节\n13:30-14:15\n    六节\n14:25-15:10";
                e.Row.MinHeight = 125;
                e.Row.MaxHeight = 125;
                _i++;
            }
            else if (_i == 3)
            {
                e.Row.Header = "    七节\n15:30-16:15\n    八节\n16:25-17:00";
                e.Row.MinHeight = 125;
                e.Row.MaxHeight = 125;
                _i++;
            }
            else if (_i == 4)
            {
                e.Row.Header = "    九节\n18:20-19:05\n    十节\n19:15-20:00";
                e.Row.MinHeight = 125;
                e.Row.MaxHeight = 125;
                _i++;
                //DataGridCell dgc = DataGridPlus.GetCell(DG, 4, 1);
            }
            else if (_i % 5 == 0)
            {
                e.Row.Header = "    一节\n8:20-9:05\n    二节\n9:15-10:00";
                e.Row.MinHeight = ActualHeight / 5 - 13;
                e.Row.MaxHeight = ActualHeight / 5 - 13;
                _i++;
            }
            else if (_i % 5 == 1)
            {
                e.Row.Header = "    三节\n10:20-11:05\n    四节\n11:15-12:00";
                e.Row.MinHeight = ActualHeight / 5 - 13;
                e.Row.MaxHeight = ActualHeight / 5 - 13;
                _i++;
            }
            else if (_i % 5 == 2)
            {
                e.Row.Header = "    五节\n13:30-14:15\n    六节\n14:25-15:10";
                e.Row.MinHeight = ActualHeight / 5 - 13;
                e.Row.MaxHeight = ActualHeight / 5 - 13;
                _i++;
            }
            else if (_i % 5 == 3)
            {
                e.Row.Header = "    七节\n15:30-16:15\n    八节\n16:25-17:00";
                e.Row.MinHeight = ActualHeight / 5 - 13;
                e.Row.MaxHeight = ActualHeight / 5 - 13;
                _i++;
            }
            else if (_i % 5 == 4)
            {
                e.Row.Header = "    九节\n18:20-19:05\n    十节\n19:15-20:00";
                e.Row.MinHeight = ActualHeight / 5 - 13;
                e.Row.MaxHeight = ActualHeight / 5 - 13;
                _i++;
                //DataGridCell dgc = DataGridPlus.GetCell(DG, 4, 1);
            }
        }

        private void ZYCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // MessageBox.Show(CbProfessional.SelectedValue.ToString());
            List<ClassInfo> classNum = new List<ClassInfo>();
            #region  数据库操作
            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();

                try
                {
                    using (new SQLiteCommand(conn))
                    {
                        string sql = "SELECT * FROM Class_Info where Grade ='" + CbClass.SelectedValue + "' and major='" + CbProfessional.SelectedValue + "'";
                        if (CbProfessional.SelectedValue.ToString() == "全部")                                //如果选择全部的话就显示出当前年级全部的班级
                        {
                            sql = "SELECT * FROM Class_Info where Grade ='" + CbClass.SelectedValue + "'";
                        }
                        var command = new SQLiteCommand(sql, conn);                                           //查询班级需要信息
                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            ClassInfo item = new ClassInfo();
                            item.Class_ID = reader["Class_ID"].ToString();
                            item.Grade = reader["Grade"].ToString();
                            item.Major = reader["major"].ToString();
                            item.Class = reader["Class"].ToString();
                            item.ClassNum = reader["Class_Num"].ToString();
                            item.ClassAllInfo = reader["Grade"].ToString() + reader["major"] + reader["Class"];
                            classNum.Add(item);//将年级专业班级信息拼接成一个完整班级信息显示在前台listbox当中
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored 
                }
            }
            #endregion
            LbClassInfo.ItemsSource = classNum;
        }

        int xueshicount(string classtype)
        {
            var selectedTp = LbCourse.SelectedValue as TeachingProject;
            ClassInfo selectClassID = null;
            if (LbClassInfo.SelectedValue != null)
            {
                selectClassID = LbClassInfo.SelectedValue as ClassInfo;
            }
            else
            {
                return 0;
            }


            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();
                if (classtype == "其他")
                {
                    int qita = 0;
                    using (new SQLiteCommand(conn))
                    {
                        string sql = "SELECT count(*) as info FROM Configuration_Info,Classroom_Info WHERE  Configuration_Info.Class_ID = '" + selectClassID.Class_ID + "' AND Configuration_Info.Course_ID = '" + selectedTp.CourseId + "' AND Classroom_Type != '机房' and Classroom_Type != '多媒体' AND Configuration_Info.Classroom_ID = Classroom_Info.Classroom_ID";
                        var command = new SQLiteCommand(sql, conn);
                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            qita = qita + Convert.ToInt32(reader["info"]) * 2;
                        }
                    }

                    //using (new SQLiteCommand(conn))
                    //{
                    //    string sql = "SELECT count(*) as info FROM Configuration_Info,Classroom_Info WHERE  Configuration_Info.Class_ID = '" + selectClassID.Class_ID + "' AND Configuration_Info.Course_ID = '" + selectedTp.CourseId + "' AND Classroom_Type = '普通教室' AND Configuration_Info.Classroom_ID = Classroom_Info.Classroom_ID";
                    //    var command = new SQLiteCommand(sql, conn);                 
                    //    var reader = command.ExecuteReader();
                    //    while (reader.Read())
                    //    {
                    //        qita = qita + Convert.ToInt32(reader["info"]) * 2;
                    //    }
                    //}

                    return qita;
                }

                if (classtype != "")
                {
                    using (new SQLiteCommand(conn))
                    {
                        string sql = "SELECT count(*) as info FROM Configuration_Info,Classroom_Info WHERE  Configuration_Info.Class_ID = '" + selectClassID.Class_ID + "' AND Configuration_Info.Course_ID = '" + selectedTp.CourseId + "' AND Classroom_Type = '" + classtype + "' AND Configuration_Info.Classroom_ID = Classroom_Info.Classroom_ID";
                        var command = new SQLiteCommand(sql, conn);                 //查询课程的详细信息
                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            return Convert.ToInt32(reader["info"]) * 2;
                        }
                    }
                }



                try
                {
                    using (new SQLiteCommand(conn))
                    {
                        string sql = "SELECT count(*) as info FROM Configuration_Info WHERE  Configuration_Info.Class_ID = '" + selectClassID.Class_ID + "' AND Configuration_Info.Course_ID = '" + selectedTp.CourseId + "'";
                        var command = new SQLiteCommand(sql, conn);
                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            return Convert.ToInt32(reader["info"]) * 2;
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored
                }

                return 0;
            }
        }

        private void lb1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (LbCourse.SelectedValue == null)
            {
                return;
            }

            //MessageBox.Show(LbCourse.SelectedValue.ToString());
            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();

                try
                {
                    using (new SQLiteCommand(conn))
                    {
                        var selectedTp = LbCourse.SelectedValue as TeachingProject;
                        //var selectClassID = LbClassInfo.SelectedValue as ClassInfo;

                        if (selectedTp != null)
                        {
                            string sql = "SELECT * FROM Lesson_Info,Lesson_Teacher,Teacher_Info where Teacher_Info.Teacher_ID = Lesson_Teacher.Teacher_ID and Lesson_Info.Course_ID = Lesson_Teacher.Course_ID and Lesson_Info.Course_ID = '" + selectedTp.CourseId + "'";
                            var command = new SQLiteCommand(sql, conn);                 //查询课程的详细信息
                            var reader = command.ExecuteReader();
                            while (reader.Read())
                            {
                                TbCourseInfo.Text = "课程名称：" + reader["Course_Name"] + "\n" +
                                                    "课程类型：" + reader["Course_Type"] + "\n" +
                                                    "教师名称：" + reader["Teacher_Name"] + "\n" +
                                                    "教学周：" + reader["Teach_Week"] + "\n" +
                                                    "课程人数：" + reader["Preson_Num"] + "\n" +
                                                    "理论学时：" + reader["Theory_Class"] + "\n" +
                                                    "试验学时：" + reader["Experiment_Class"] + "\n" +
                                                    "总学时：" + reader["Total_Class"] + "\n" +
                                                    "已排：" + xueshicount("") + "\n" +
                                                    "每周应上学时：" + reader["Week_Class"] + "\n" +
                                                    "机房学时：" + reader["jifang_Num"] + "\n" +
                                                    "已排：" + xueshicount("机房") + "\n" +
                                                    "教室学时：" + reader["jiaoshi_Num"] + "\n" +
                                                    "已排：" + xueshicount("多媒体") + "\n" +
                                                    "其它学时：" + reader["Qita_Num"] + "\n" +
                                                    "已排：" + xueshicount("其他") + "\n" +
                                                    "课程备注：" + reader["Class_Note"] + "\n" +
                                                    "教师备注：" + reader["Teacher_Note"];
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored
                }


                try                         //选择课程，班级信息联动变化
                {
                    string grade = "";
                    string major = "";
                    using (new SQLiteCommand(conn))
                    {

                        //查询课程所对应的班级的年级，专业



                        //如果与combox不符合就重新加载班级listbox
                        var selectedTp = LbCourse.SelectedValue as TeachingProject;

                        if (selectedTp != null)
                        {

                            string sql = "select Grade,major from Course_Class where Course_ID = '" + selectedTp.CourseId + "'";
                            var command = new SQLiteCommand(sql, conn);                 //查询课程的详细信息
                            var reader = command.ExecuteReader();
                            while (reader.Read())
                            {
                                grade = reader["Grade"].ToString();
                                major = reader["Major"].ToString();
                            }


                        }
                    }
                    ClassInfo aa = new ClassInfo();
                    foreach (var item in LbClassInfo.SelectedItems)
                    {
                        aa = item as ClassInfo;
                    }
                    if (aa != null && (grade != aa.Grade || major != aa.Major && CbProfessional.SelectedValue.ToString() != "全部"))
                    {
                        List<ClassInfo> classNum = new List<ClassInfo>();
                        using (new SQLiteCommand(conn))
                        {

                            string sql = "SELECT * FROM Class_Info where Grade ='" + grade + "' and major='" + major + "'";

                            var command = new SQLiteCommand(sql, conn);                                           //查询班级需要信息
                            var reader = command.ExecuteReader();
                            while (reader.Read())
                            {
                                ClassInfo item = new ClassInfo();
                                item.Class_ID = reader["Class_ID"].ToString();
                                item.Grade = reader["Grade"].ToString();
                                item.Major = reader["major"].ToString();
                                item.Class = reader["Class"].ToString();
                                item.ClassNum = reader["Class_Num"].ToString();
                                item.ClassAllInfo = reader["Grade"].ToString() + reader["major"] + reader["Class"];
                                classNum.Add(item);//将年级专业班级信息拼接成一个完整班级信息显示在前台listbox当中
                            }
                        }
                        LbClassInfo.ItemsSource = classNum;
                    }

                }
                catch (Exception)
                {
                    // ignored
                }


            }




        }

        private void LbClassInfo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            #region 初始化前台课表内容
            _list[0].didian1 = "";
            _list[0].didian2 = "";
            _list[0].didian3 = "";
            _list[0].didian4 = "";
            _list[0].didian5 = "";
            _list[0].lesson1 = "";
            _list[0].lesson2 = "";
            _list[0].lesson3 = "";
            _list[0].lesson4 = "";
            _list[0].lesson5 = "";
            _list[0].teacher1 = "";
            _list[0].teacher2 = "";
            _list[0].teacher3 = "";
            _list[0].teacher4 = "";
            _list[0].teacher5 = "";
            _list[0].week1 = "";
            _list[0].week2 = "";
            _list[0].week3 = "";
            _list[0].week4 = "";
            _list[0].week5 = "";

            _list[1].didian1 = "";
            _list[1].didian2 = "";
            _list[1].didian3 = "";
            _list[1].didian4 = "";
            _list[1].didian5 = "";
            _list[1].lesson1 = "";
            _list[1].lesson2 = "";
            _list[1].lesson3 = "";
            _list[1].lesson4 = "";
            _list[1].lesson5 = "";
            _list[1].teacher1 = "";
            _list[1].teacher2 = "";
            _list[1].teacher3 = "";
            _list[1].teacher4 = "";
            _list[1].teacher5 = "";
            _list[1].week1 = "";
            _list[1].week2 = "";
            _list[1].week3 = "";
            _list[1].week4 = "";
            _list[1].week5 = "";

            _list[2].didian1 = "";
            _list[2].didian2 = "";
            _list[2].didian3 = "";
            _list[2].didian4 = "";
            _list[2].didian5 = "";
            _list[2].lesson1 = "";
            _list[2].lesson2 = "";
            _list[2].lesson3 = "";
            _list[2].lesson4 = "";
            _list[2].lesson5 = "";
            _list[2].teacher1 = "";
            _list[2].teacher2 = "";
            _list[2].teacher3 = "";
            _list[2].teacher4 = "";
            _list[2].teacher5 = "";
            _list[2].week1 = "";
            _list[2].week2 = "";
            _list[2].week3 = "";
            _list[2].week4 = "";
            _list[2].week5 = "";

            _list[3].didian1 = "";
            _list[3].didian2 = "";
            _list[3].didian3 = "";
            _list[3].didian4 = "";
            _list[3].didian5 = "";
            _list[3].lesson1 = "";
            _list[3].lesson2 = "";
            _list[3].lesson3 = "";
            _list[3].lesson4 = "";
            _list[3].lesson5 = "";
            _list[3].teacher1 = "";
            _list[3].teacher2 = "";
            _list[3].teacher3 = "";
            _list[3].teacher4 = "";
            _list[3].teacher5 = "";
            _list[3].week1 = "";
            _list[3].week2 = "";
            _list[3].week3 = "";
            _list[3].week4 = "";
            _list[3].week5 = "";

            _list[4].didian1 = "";
            _list[4].didian2 = "";
            _list[4].didian3 = "";
            _list[4].didian4 = "";
            _list[4].didian5 = "";
            _list[4].lesson1 = "";
            _list[4].lesson2 = "";
            _list[4].lesson3 = "";
            _list[4].lesson4 = "";
            _list[4].lesson5 = "";
            _list[4].teacher1 = "";
            _list[4].teacher2 = "";
            _list[4].teacher3 = "";
            _list[4].teacher4 = "";
            _list[4].teacher5 = "";
            _list[4].week1 = "";
            _list[4].week2 = "";
            _list[4].week3 = "";
            _list[4].week4 = "";
            _list[4].week5 = "";

            #endregion
            if (LbClassInfo.SelectedItem == null)
            {
                DgCurriculum.ItemsSource = null;
                DgCurriculum.ItemsSource = _list;
                return;
            }
            //MessageBox.Show(LbClassInfo.SelectedItem.ToString());
            //MessageBox.Show(CbProfessional.SelectedItem.ToString());
            using (var conn1 = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn1.ConnectionString = connsb.ToString();
                conn1.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn1))
                {
                    var classId = LbClassInfo.SelectedItem as ClassInfo;
                    //MessageBox.Show(match.Value);
                    if (classId != null)
                    {
                        string sql1 = string.Format("select Course_Name,Classroom_Name,Teacher_Name,Teaching_Time from Configuration_Info, Lesson_Info, Classroom_Info, Teacher_Info, Class_Info where Configuration_Info.Course_ID = Lesson_Info.Course_ID and Configuration_Info.Teacher_ID = Teacher_Info.Teacher_ID and Configuration_Info.Classroom_ID = Classroom_Info.Classroom_ID and Configuration_Info.Class_ID = Class_Info.Class_ID and major = '{0}' and Class = '{1}' and Grade = '{2}' order BY Teaching_Time ASC,Course_Name ASC", classId.Major, classId.Class, classId.Grade);
                        cmd.CommandText = sql1;
                    }
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(cmd);
                    DataSet data = new DataSet();
                    adapter.Fill(data);
                    DataTable table = data.Tables[0];
                    foreach (DataRow tr in table.Rows)
                    {
                        Lesson l = new Lesson();
                        if (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10 == 1)
                        {
                            l = _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1];
                            string pat = string.Format(@"{0}$", tr["Classroom_Name"]);
                            Match match1 = Regex.Match(_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian1, pat);
                            if (match1.Value == tr["Classroom_Name"].ToString()) //暂使用教室匹配解决
                            {
                                //l.didian1 = tr["Classroom_Name"].ToString();
                                //l.lesson1 = tr["Course_Name"].ToString();
                                l.week1 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //l.teacher1 = tr["Teacher_Name"].ToString();
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson1 != tr["Course_Name"].ToString() &&
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson1 != "")
                            {
                                l.didian1 += "/" + tr["Classroom_Name"];
                                l.lesson1 += "/" + tr["Course_Name"];
                                l.week1 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher1 += "/" + tr["Teacher_Name"];
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian1 != tr["Classroom_Name"].ToString() &&
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian1 != "" &&
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson1 == tr["Course_Name"].ToString())
                            {
                                l.didian1 += "/" + tr["Classroom_Name"];
                                l.lesson1 += "/" + tr["Course_Name"];
                                l.week1 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher1 += "/" + tr["Teacher_Name"];
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (/*_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].week1 == (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10).ToString() &&*/
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher1 == tr["Teacher_Name"].ToString() &&
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson1 == tr["Course_Name"].ToString()
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher1 != "")
                            {
                                //if (Convert.ToInt32(l.week1) > (Convert.ToInt32(tr["Teaching_Time"]) / 100))
                                //{
                                //    string temp = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //    temp += "," + l.week1;
                                //    l.week1 = temp;
                                //}
                                //else
                                //{
                                l.didian1 = tr["Classroom_Name"].ToString();
                                l.lesson1 = tr["Course_Name"].ToString();
                                l.week1 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                l.teacher1 = tr["Teacher_Name"].ToString();
                                //}
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            l.didian1 = tr["Classroom_Name"].ToString();
                            l.lesson1 = tr["Course_Name"].ToString();
                            l.week1 = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                            l.teacher1 = tr["Teacher_Name"].ToString();
                        }
                        else if (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10 == 2)
                        {
                            l = _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1];
                            string pat = string.Format(@"{0}$", tr["Classroom_Name"]);
                            Match match1 = Regex.Match(_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian2, pat);
                            if (match1.Value == tr["Classroom_Name"].ToString())
                            {
                                //l.didian2 = tr["Classroom_Name"].ToString();
                                //l.lesson2 = tr["Course_Name"].ToString();
                                l.week2 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //l.teacher2 = tr["Teacher_Name"].ToString();
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson2 != tr["Course_Name"].ToString() &&
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson2 != "")
                            {
                                l.didian2 += "/" + tr["Classroom_Name"];
                                l.lesson2 += "/" + tr["Course_Name"];
                                l.week2 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher2 += "/" + tr["Teacher_Name"];
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian2 != tr["Classroom_Name"].ToString() &&
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian2 != "" &&
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson2 == tr["Course_Name"].ToString())
                            {
                                l.didian2 += "/" + tr["Classroom_Name"];
                                l.lesson2 += "/" + tr["Course_Name"];
                                l.week2 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher2 += "/" + tr["Teacher_Name"];
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (/*_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].week1 == (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10).ToString() &&*/
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher2 == tr["Teacher_Name"].ToString() &&
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson2 == tr["Course_Name"].ToString()
                                   _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher2 != "")
                            {
                                //if (Convert.ToInt32(l.week2) > (Convert.ToInt32(tr["Teaching_Time"]) / 100))
                                //{
                                //    string temp = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //    temp += "," + l.week2;
                                //    l.week2 = temp;
                                //}
                                //else
                                //{
                                l.didian2 = tr["Classroom_Name"].ToString();
                                l.lesson2 = tr["Course_Name"].ToString();
                                l.week2 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                l.teacher2 = tr["Teacher_Name"].ToString();
                                //}
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            l.didian2 = tr["Classroom_Name"].ToString();
                            l.lesson2 = tr["Course_Name"].ToString();
                            l.week2 = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                            l.teacher2 = tr["Teacher_Name"].ToString();
                        }
                        else if (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10 == 3)
                        {
                            l = _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1];
                            string pat = string.Format(@"{0}$", tr["Classroom_Name"]);
                            Match match1 = Regex.Match(_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian3, pat);
                            if (match1.Value == tr["Classroom_Name"].ToString())
                            {
                                //l.didian3 = tr["Classroom_Name"].ToString();
                                //l.lesson3 = tr["Course_Name"].ToString();
                                l.week3 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //l.teacher3 = tr["Teacher_Name"].ToString();
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson3 != tr["Course_Name"].ToString() &&
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson3 != "")
                            {
                                l.didian3 += "/" + tr["Classroom_Name"];
                                l.lesson3 += "/" + tr["Course_Name"];
                                l.week3 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher3 += "/" + tr["Teacher_Name"];
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian3 != tr["Classroom_Name"].ToString() &&
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian3 != "" &&
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson3 == tr["Course_Name"].ToString())
                            {
                                l.didian3 += "/" + tr["Classroom_Name"];
                                l.lesson3 += "/" + tr["Course_Name"];
                                l.week3 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher3 += "/" + tr["Teacher_Name"];
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (/*_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].week1 == (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10).ToString() &&*/
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher3 == tr["Teacher_Name"].ToString() &&
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson3 == tr["Course_Name"].ToString()
                                   _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher3 != "")
                            {
                                //if (Convert.ToInt32(l.week3) > (Convert.ToInt32(tr["Teaching_Time"]) / 100))
                                //{
                                //    string temp = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //    temp += "," + l.week3;
                                //    l.week3 = temp;
                                //}
                                //else
                                //{
                                l.didian3 = tr["Classroom_Name"].ToString();
                                l.lesson3 = tr["Course_Name"].ToString();
                                l.week3 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                l.teacher3 = tr["Teacher_Name"].ToString();
                                //}
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            l.didian3 = tr["Classroom_Name"].ToString();
                            l.lesson3 = tr["Course_Name"].ToString();
                            l.week3 = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                            l.teacher3 = tr["Teacher_Name"].ToString();
                        }
                        else if (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10 == 4)
                        {
                            l = _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1];
                            string pat = string.Format(@"{0}$", tr["Classroom_Name"]);
                            Match match1 = Regex.Match(_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian4, pat);
                            if (match1.Value == tr["Classroom_Name"].ToString())
                            {
                                //l.didian4 = tr["Classroom_Name"].ToString();
                                //l.lesson4 = tr["Course_Name"].ToString();
                                l.week4 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                //l.teacher4 = tr["Teacher_Name"].ToString();
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson4 != tr["Course_Name"].ToString() &&
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson4 != "")
                            {
                                l.didian4 += "/" + tr["Classroom_Name"];
                                l.lesson4 += "/" + tr["Course_Name"];
                                l.week4 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher4 += "/" + tr["Teacher_Name"];
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian4 != tr["Classroom_Name"].ToString() &&
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian4 != "" &&
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson4 == tr["Course_Name"].ToString())
                            {
                                l.didian4 += "/" + tr["Classroom_Name"];
                                l.lesson4 += "/" + tr["Course_Name"];
                                l.week4 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher4 += "/" + tr["Teacher_Name"];
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (/*_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].week1 == (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10).ToString() &&*/
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher4 == tr["Teacher_Name"].ToString() &&
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson4 == tr["Course_Name"].ToString()
                                   _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher4 != "")
                            {
                                //if (Convert.ToInt32(l.week4) > (Convert.ToInt32(tr["Teaching_Time"]) / 100))
                                //{
                                //    string temp = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //    temp += "," + l.week4;
                                //    l.week4 = temp;
                                //}
                                //else
                                //{
                                l.didian4 = tr["Classroom_Name"].ToString();
                                l.lesson4 = tr["Course_Name"].ToString();
                                l.week4 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher4 = tr["Teacher_Name"].ToString();
                                //}
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            l.didian4 = tr["Classroom_Name"].ToString();
                            l.lesson4 = tr["Course_Name"].ToString();
                            l.week4 = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                            l.teacher4 = tr["Teacher_Name"].ToString();
                        }
                        else if (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10 == 5)
                        {
                            l = _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1];
                            string pat = string.Format(@"{0}$", tr["Classroom_Name"]);
                            Match match1 = Regex.Match(_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian5, pat);
                            if (match1.Value == tr["Classroom_Name"].ToString())
                            {
                                //l.didian5 = tr["Classroom_Name"].ToString();
                                //l.lesson5 = tr["Course_Name"].ToString();
                                l.week5 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //l.teacher5 = tr["Teacher_Name"].ToString();
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson5 != tr["Course_Name"].ToString() &&
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson5 != "")
                            {
                                l.didian5 += "/" + tr["Classroom_Name"];
                                l.lesson5 += "/" + tr["Course_Name"];
                                l.week5 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher5 += "/" + tr["Teacher_Name"];
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian5 != tr["Classroom_Name"].ToString() &&
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian5 != "" &&
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson5 == tr["Course_Name"].ToString())
                            {
                                l.didian5 += "/" + tr["Classroom_Name"];
                                l.lesson5 += "/" + tr["Course_Name"];
                                l.week5 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher5 += "/" + tr["Teacher_Name"];
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (/*_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].week1 == (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10).ToString() &&*/
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher5 == tr["Teacher_Name"].ToString() &&
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson5 == tr["Course_Name"].ToString()
                                   _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher5 != "")
                            {
                                //if (Convert.ToInt32(l.week5) > (Convert.ToInt32(tr["Teaching_Time"]) / 100))
                                //{
                                //    string temp = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //    temp += "," + l.week5;
                                //    l.week5 = temp;
                                //}
                                //else
                                //{
                                l.didian5 = tr["Classroom_Name"].ToString();
                                l.lesson5 = tr["Course_Name"].ToString();
                                l.week5 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                l.teacher5 = tr["Teacher_Name"].ToString();
                                //}
                                _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            l.didian5 = tr["Classroom_Name"].ToString();
                            l.lesson5 = tr["Course_Name"].ToString();
                            l.week5 = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                            l.teacher5 = tr["Teacher_Name"].ToString();
                        }
                        _list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                    }
                }
            }
            for (int i = 0; i < 5; i++)
            {
                _list[i].week1 = strChange(_list[i].week1);
                _list[i].week2 = strChange(_list[i].week2);
                _list[i].week3 = strChange(_list[i].week3);
                _list[i].week4 = strChange(_list[i].week4);
                _list[i].week5 = strChange(_list[i].week5);
            }

            DgCurriculum.ItemsSource = null;
            DgCurriculum.ItemsSource = _list;

            //初始化课程列表，把相应的课程提前到前面
            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();
                using (new SQLiteCommand(conn))
                {
                    ClassInfo aa = new ClassInfo();
                    foreach (var item in LbClassInfo.SelectedItems)
                    {
                        aa = item as ClassInfo;
                    }

                    List<TeachingProject> classInfo1 = new List<TeachingProject>();
                    if (aa != null)
                    {
                        string sql = "SELECT * FROM Lesson_Info,Course_Class WHERE Lesson_Info.Course_ID = Course_Class.Course_ID AND Grade ='" + aa.Grade + "' and major='" + aa.Major + "'";
                        if (CbProfessional.SelectedValue.ToString() == "全部")
                        {
                            sql = "SELECT * FROM Lesson_Info,Course_Class WHERE Lesson_Info.Course_ID = Course_Class.Course_ID AND Grade ='" + CbClass.SelectedValue + "'";
                        }
                        var command = new SQLiteCommand(sql, conn);
                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            var tp = new TeachingProject
                            {

                                CourseName = reader["Course_Name"].ToString(),
                                CourseId = reader["Course_ID"].ToString(),
                                TotalHours = reader["Total_Class"].ToString(),
                                /*WeekOfTeaching = reader["Teaching_Week"].ToString(),*/
                                UseComputerRoom = reader["jifang_Num"].ToString(),
                                UseClassroomHours = reader["jiaoshi_Num"].ToString(),
                                UsePcClassroom = reader["qita_Num"].ToString()
                            };
                            classInfo1.Add(tp);
                        }
                    }
                    var tpi = new TeachingProject
                    {
                        CourseName = "--------分割线----------"
                    };
                    classInfo1.Add(tpi);
                    foreach (var item in _classInfo)
                    {
                        classInfo1.Add(item);
                    }
                    LbCourse.ItemsSource = null;
                    LbCourse.ItemsSource = classInfo1;

                }
            }



        }

        private void bt_Click_Configuration(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show("1,1");
            //MessageBox.Show(bjlb.SelectedItems.ToString());
            List<Array> classList = new List<Array>();
            foreach (var item in LbClassInfo.SelectedItems)//b班级
            {
                var aa = item as ClassInfo;
                if (aa != null)
                {
                    string[] text = new[] { aa.Grade, aa.Major, aa.Class };

                    classList.Add(text);
                }
                //MessageBox.Show(aa.ToString());
            }
            //MessageBox.Show(LbCourse.SelectedValue.ToString());//课程
            string classId = null;//教室
            try
            {
                if (TcClassInfo.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:多媒体教室 Content:")
                {
                    classId = LbMulClassroom.SelectedValue.ToString();
                }
                if (TcClassInfo.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:笔记本教室 Content:")
                {
                    classId = LbLapClassroom.SelectedValue.ToString();
                }
                if (TcClassInfo.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:机房教室 Content:")
                {
                    classId = LbMacClasseoom.SelectedValue.ToString();
                }
                if (TcClassInfo.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:普通教室 Content:")
                {
                    classId = LbComClasseoom.SelectedValue.ToString();
                }
            }
            catch (Exception)
            {
                //
            }

            //MessageBox.Show(classId);

            var buttonName = sender as Button;
            if (buttonName != null)
            {
                string teachTime = Regex.Replace(buttonName.Name, @"[^\d]+", "");//时间

                //MessageBox.Show(teachTime);
                var selectedTp = LbCourse.SelectedValue as TeachingProject;
                //if (classList != null || selectedTp.CourseId != null || classId != null || teachTime != null)
                //{
                //    MessageBox.Show("排课信息选择不完整");
                //    return;
                //}
                if (TbCourseInfo.Text == "")
                {
                    MessageBox.Show("排课信息选择不全！");
                    return;
                }
                if (selectedTp == null)
                {
                    MessageBox.Show("请选择一门课程哦！");
                    return;
                }
                Confirm abc = new Confirm(classList, selectedTp, classId, teachTime);

                abc.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                abc.Owner = this;
                abc.ShowDialog();
                LbClassInfo_SelectionChanged(sender, e as SelectionChangedEventArgs);      //刷新界面的课表信息
            }
        }

        private void MenuItem_OnClick(object sender, RoutedEventArgs e)
        {
            var buttonName = sender as MenuItem;
            var classId = LbClassInfo.SelectedItem as ClassInfo;
            //string pattern = @"\d$";
            //Match match = Regex.Match(buttonName.Name, pattern);
            //pattern = @"\d";
            //Match match1 = Regex.Match(buttonName.Name, pattern);
            if (classId != null && buttonName != null)
            {
                Delete_Class dc = new Delete_Class(classId.Grade, classId.Major, classId.Class, buttonName.Name);
                dc.Grade = classId.Grade;
                dc.Major = classId.Major;
                dc.Class = classId.Class;
                dc.Time = buttonName.Name;
                dc.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                dc.Owner = this;
                dc.ShowDialog();
            }
            LbClassInfo_SelectionChanged(sender, e as SelectionChangedEventArgs);
            //using (var conn = new SQLiteConnection())
            //{
            //    var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
            //    conn.ConnectionString = connsb.ToString();
            //    conn.Open();
            //    using (new SQLiteCommand(conn))
            //    {
            //        string pattern = @"\d+$";
            //        Match match = Regex.Match(LbClassInfo.SelectedItem.ToString(), pattern);
            //        var classID = LbClassInfo.SelectedItem as ClassInfo;
            //        //string sql = "SELECT Class_ID FROM Class_Info where Grade ='" + CbClass.SelectedValue + "' and major='" + CbProfessional.SelectedValue + "' and Class = '"+ LbClassInfo.SelectedItem+ "'"; //查询数据库当中所选择对应的班级编号
            //        if (buttonName != null)
            //        {
            //            string sql = "DELETE from Configuration_Info WHERE Class_ID = (SELECT Class_ID FROM Class_Info where Grade = '" + CbClass.SelectedValue + "' and major = '" + CbProfessional.SelectedValue + "' and Class = '" + classID.Class + "' ) and Teaching_Time like '" + buttonName.Name + "'";
            //            var command = new SQLiteCommand(sql, conn);
            //            var reader = command.ExecuteReader();
            //            while (reader.Read())
            //            {
            //                MessageBox.Show("删除完成");
            //            }
            //        }
            //    }
            //}
            //MessageBox.Show("课程删除完成");
            //LbClassInfo_SelectionChanged(sender, e as SelectionChangedEventArgs);      //刷新界面的课表信息
        }

        string strChange(string str)
        {
            if (str == "")
            {
                return "";
            }
            if (str.Length <= 2)
            {
                return str;
            }

            List<string> aList1 = new List<string>();
            int i;

            if (str.Substring(1, 1) == "," || str.Substring(1, 1) == "/")
            {
                aList1.Add(str.Substring(0, 1));
                i = 2;
            }
            else
            {
                aList1.Add(str.Substring(0, 1) + str.Substring(1, 1));
                i = 3;
            }

            for (; i < str.Length; i++)
            {
                if (str.Substring(i, 1) == "," || str.Substring(i, 1) == "/")
                {

                    if (str.Substring(i - 1, 1) != "," && str.Substring(i - 1, 1) != "/")
                    {
                        if (str.Substring(i - 2, 1) != "," && str.Substring(i - 2, 1) != "/")
                        {
                            aList1.Add(str.Substring(i - 2, 1) + str.Substring(i - 1, 1));
                        }
                        else
                        {
                            aList1.Add(str.Substring(i - 1, 1));
                        }
                    }

                    if (str.Substring(i, 1) == "/")
                    {
                        aList1.Add(str.Substring(i, 1));
                    }
                }
            }

            if (str.Substring(str.Length - 2, 1) == "," || str.Substring(str.Length - 2, 1) == "/")
            {
                aList1.Add(str.Substring(str.Length - 1, 1));

            }
            else
            {
                aList1.Add(str.Substring(str.Length - 2, 1) + str.Substring(str.Length - 1, 1));

            }

            for (i = 0; i < aList1.Count - 1; i++)
            {
                if (aList1[i] != "/" && aList1[i + 1] != "/" && aList1[i] != "," && aList1[i + 1] != ",")
                {
                    if (Convert.ToInt32(aList1[i]) + 1 != Convert.ToInt32(aList1[i + 1]))
                    {
                        aList1.Insert(i + 1, ",");
                    }
                }
            }
            string res = aList1[0];
            for (i = 1; i < aList1.Count; i++)
            {
                if (aList1[i] == ",")
                {
                    res = res + "-" + aList1[i - 1] + ",";

                }
                if (aList1[i - 1] == ",")
                {
                    res = res + aList1[i];
                }
                if (aList1[i] == "/")
                {
                    res = res + "-" + aList1[i - 1] + "/";

                }
                if (aList1[i - 1] == "/")
                {
                    res = res + aList1[i];
                }
            }

            res = res + "-" + aList1[aList1.Count - 1];

            return res;

            //字符串变成list，每个格子存放数字，去掉逗号

            //遍历数字，两个相邻的数字而且不相差一就加入逗号

            //遍历字符串拼接出结果

        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (WindowState == WindowState.Maximized)
            {
                g1.Height = ActualHeight;
                g1.Width = ActualWidth - 350;
                LbCourse.Height = ActualHeight - 310;
                TbCourseInfo.Height = ActualHeight - 310;

                BtClass11.Height = ActualHeight / 5 - 18;
                BtClass12.Height = ActualHeight / 5 - 18;
                BtClass13.Height = ActualHeight / 5 - 17;
                BtClass14.Height = ActualHeight / 5 - 18;
                BtClass15.Height = ActualHeight / 5 - 18;

                BtClass21.Height = ActualHeight / 5 - 18;
                BtClass22.Height = ActualHeight / 5 - 18;
                BtClass23.Height = ActualHeight / 5 - 17;
                BtClass24.Height = ActualHeight / 5 - 18;
                BtClass25.Height = ActualHeight / 5 - 18;

                BtClass31.Height = ActualHeight / 5 - 18;
                BtClass32.Height = ActualHeight / 5 - 18;
                BtClass33.Height = ActualHeight / 5 - 17;
                BtClass34.Height = ActualHeight / 5 - 18;
                BtClass35.Height = ActualHeight / 5 - 18;

                BtClass41.Height = ActualHeight / 5 - 18;
                BtClass42.Height = ActualHeight / 5 - 18;
                BtClass43.Height = ActualHeight / 5 - 17;
                BtClass44.Height = ActualHeight / 5 - 18;
                BtClass45.Height = ActualHeight / 5 - 18;

                BtClass51.Height = ActualHeight / 5 - 18;
                BtClass52.Height = ActualHeight / 5 - 18;
                BtClass53.Height = ActualHeight / 5 - 17;
                BtClass54.Height = ActualHeight / 5 - 18;
                BtClass55.Height = ActualHeight / 5 - 18;

                //BtClass12.Margin = new Thickness(133, (SystemParameters.PrimaryScreenHeight / 5) + (SystemParameters.PrimaryScreenHeight / 41), 0, 0);
                //BtClass13.Margin = new Thickness(133, ((SystemParameters.PrimaryScreenHeight / 5) * 2) + (SystemParameters.PrimaryScreenHeight / 84), 0, 0);
                //BtClass14.Margin = new Thickness(133, ((SystemParameters.PrimaryScreenHeight / 5) * 3) - (SystemParameters.PrimaryScreenHeight * 0.0015), 0, 0);
                //BtClass15.Margin = new Thickness(133, ((SystemParameters.PrimaryScreenHeight / 5) * 3.925), 0, 0);

                //BtClass21.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 1.15, 28, 0, 0);
                //BtClass22.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 1.15, (SystemParameters.PrimaryScreenHeight / 5) + (SystemParameters.PrimaryScreenHeight / 41), 0, 0);
                //BtClass23.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 1.15, ((SystemParameters.PrimaryScreenHeight / 5) * 2) + (SystemParameters.PrimaryScreenHeight / 84), 0, 0);
                //BtClass24.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 1.15, ((SystemParameters.PrimaryScreenHeight / 5) * 3) - (SystemParameters.PrimaryScreenHeight * 0.0015), 0, 0);
                //BtClass25.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 1.15, ((SystemParameters.PrimaryScreenHeight / 5) * 3.925), 0, 0);

                //BtClass31.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 1.8, 28, 0, 0);
                //BtClass32.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 1.8, (SystemParameters.PrimaryScreenHeight / 5) + (SystemParameters.PrimaryScreenHeight / 41), 0, 0);
                //BtClass33.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 1.8, ((SystemParameters.PrimaryScreenHeight / 5) * 2) + (SystemParameters.PrimaryScreenHeight / 84), 0, 0);
                //BtClass34.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 1.8, ((SystemParameters.PrimaryScreenHeight / 5) * 3) - (SystemParameters.PrimaryScreenHeight * 0.0015), 0, 0);
                //BtClass35.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 1.8, ((SystemParameters.PrimaryScreenHeight / 5) * 3.925), 0, 0);

                //BtClass41.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 2.46, 28, 0, 0);
                //BtClass42.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 2.46, (SystemParameters.PrimaryScreenHeight / 5) + (SystemParameters.PrimaryScreenHeight / 41), 0, 0);
                //BtClass43.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 2.46, ((SystemParameters.PrimaryScreenHeight / 5) * 2) + (SystemParameters.PrimaryScreenHeight / 84), 0, 0);
                //BtClass44.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 2.46, ((SystemParameters.PrimaryScreenHeight / 5) * 3) - (SystemParameters.PrimaryScreenHeight * 0.0015), 0, 0);
                //BtClass45.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 2.46, ((SystemParameters.PrimaryScreenHeight / 5) * 3.925), 0, 0);

                //BtClass51.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 3.115, 28, 0, 0);
                //BtClass52.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 3.115, (SystemParameters.PrimaryScreenHeight / 5) + (SystemParameters.PrimaryScreenHeight / 41), 0, 0);
                //BtClass53.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 3.115, ((SystemParameters.PrimaryScreenHeight / 5) * 2) + (SystemParameters.PrimaryScreenHeight / 84), 0, 0);
                //BtClass54.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 3.115, ((SystemParameters.PrimaryScreenHeight / 5) * 3) - (SystemParameters.PrimaryScreenHeight * 0.0015), 0, 0);
                //BtClass55.Margin = new Thickness((SystemParameters.PrimaryScreenWidth / 5) * 3.115, ((SystemParameters.PrimaryScreenHeight / 5) * 3.925), 0, 0);
                foreach (var item in _list)
                {
                    if (item.didian1 == "")
                    {
                        item.didian1 = " ";
                    }
                    else if (item.didian2 == "")
                    {
                        item.didian2 = " ";
                    }
                    else if (item.didian3 == "")
                    {
                        item.didian3 = " ";
                    }
                    else if (item.didian4 == "")
                    {
                        item.didian4 = " ";
                    }
                    else if (item.didian5 == "")
                    {
                        item.didian5 = " ";
                    }
                }
                DgCurriculum.ItemsSource = null;
                DgCurriculum.ItemsSource = _list;
            }
            if (WindowState == WindowState.Normal)
            {
                g1.Height = 671;
                g1.Width = 942;
                LbCourse.Height = 380;
                TbCourseInfo.Height = 380;

                //BtClass12.Margin = new Thickness(133, 153, 0, 0);
                //BtClass13.Margin = new Thickness(133, 278, 0, 0);
                //BtClass14.Margin = new Thickness(133, 403, 0, 0);
                //BtClass15.Margin = new Thickness(133, 528, 0, 0);

                //BtClass21.Margin = new Thickness(296, 28, 0, 0);
                //BtClass22.Margin = new Thickness(296, 153, 0, 0);
                //BtClass23.Margin = new Thickness(296, 278, 0, 0);
                //BtClass24.Margin = new Thickness(296, 403, 0, 0);
                //BtClass25.Margin = new Thickness(296, 528, 0, 0);

                //BtClass31.Margin = new Thickness(457, 28, 0, 0);
                //BtClass32.Margin = new Thickness(457, 153, 0, 0);
                //BtClass33.Margin = new Thickness(457, 278, 0, 0);
                //BtClass34.Margin = new Thickness(457, 403, 0, 0);
                //BtClass35.Margin = new Thickness(457, 528, 0, 0);

                //BtClass41.Margin = new Thickness(618, 28, 0, 0);
                //BtClass42.Margin = new Thickness(618, 153, 0, 0);
                //BtClass43.Margin = new Thickness(618, 278, 0, 0);
                //BtClass44.Margin = new Thickness(618, 403, 0, 0);
                //BtClass45.Margin = new Thickness(618, 528, 0, 0);

                //BtClass51.Margin = new Thickness(779, 28, 0, 0);
                //BtClass52.Margin = new Thickness(779, 153, 0, 0);
                //BtClass53.Margin = new Thickness(779, 278, 0, 0);
                //BtClass54.Margin = new Thickness(779, 403, 0, 0);
                //BtClass55.Margin = new Thickness(779, 528, 0, 0);

                BtClass11.Height = 120.5;
                BtClass12.Height = 120.5;
                BtClass13.Height = 120.5;
                BtClass14.Height = 120.5;
                BtClass15.Height = 120.5;

                BtClass21.Height = 120.5;
                BtClass22.Height = 120.5;
                BtClass23.Height = 120.5;
                BtClass24.Height = 120.5;
                BtClass25.Height = 120.5;

                BtClass31.Height = 120.5;
                BtClass32.Height = 120.5;
                BtClass33.Height = 120.5;
                BtClass34.Height = 120.5;
                BtClass35.Height = 120.5;

                BtClass41.Height = 120.5;
                BtClass42.Height = 120.5;
                BtClass43.Height = 120.5;
                BtClass44.Height = 120.5;
                BtClass45.Height = 120.5;

                BtClass51.Height = 120.5;
                BtClass52.Height = 120.5;
                BtClass53.Height = 120.5;
                BtClass54.Height = 120.5;
                BtClass55.Height = 120.5;
                if (W1.Width != 1300)
                {
                    W1.Width = 1300;
                }
                if (W1.Height != 690)
                {
                    W1.Height = 690;
                }
                foreach (var item in _list)
                {
                    if (item.didian1 == " ")
                    {
                        item.didian1 = "";
                    }
                    else if (item.didian2 == " ")
                    {
                        item.didian2 = "";
                    }
                    else if (item.didian3 == " ")
                    {
                        item.didian3 = "";
                    }
                    else if (item.didian4 == " ")
                    {
                        item.didian4 = "";
                    }
                    else if (item.didian5 == " ")
                    {
                        item.didian5 = "";
                    }
                }
                DgCurriculum.ItemsSource = null;
                DgCurriculum.ItemsSource = _list;
            }
        }

        //public static void Export(DataGrid dataGrid, string _fileName)
        //{
        //DataTable dt = new DataTable();
        //for (int i = 0; i < dataGrid.Columns.Count; i++)
        //{
        //    if (dataGrid.Columns[i].Visibility == Visibility.Visible)//只导出可见列  
        //    {
        //        dt.Columns.Add(dataGrid.Columns[i].Header.ToString());//构建表头  

        //    }
        //}

        //for (int i = 0; i < dataGrid.Items.Count; i++)
        //{
        //    int columnsIndex = 0;
        //    DataRow row = dt.NewRow();
        //    for (int j = 0; j < dataGrid.Columns.Count; j++)
        //    {
        //        if (dataGrid.Columns[j].Visibility == Visibility.Visible)
        //        {
        //            if (dataGrid.Items[i] != null && (dataGrid.Columns[j].GetCellContent(dataGrid.Items[i]) as TextBlock) != null)//填充可见列数据  
        //            {
        //                row[columnsIndex] = (dataGrid.Columns[j].GetCellContent(dataGrid.Items[i]) as TextBlock).Text.ToString();
        //            }
        //            else
        //            {
        //                row[columnsIndex] = "";
        //            }
        //            columnsIndex++;
        //        }
        //    }
        //    dt.Rows.Add(row);
        //}


        //System.Windows.Forms.SaveFileDialog save = new System.Windows.Forms.SaveFileDialog();
        //save.Filter = "Excel文件(*.xls)|";
        //save.Title = "请选择要保存的路径";
        //save.FileName = _fileName;

        //if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        //{
        //    string fileName = save.FileName;
        //    MessageBox.Show("123");
        //    //ExportFiles.ExportExcel(fileName, dt);
        //}
        //}

        public void Export(DataGrid dataGrid, string excelTitle)
        {
            //for (int i = 0; i < dataGrid.Columns.Count; i++)
            //{
            //    if (dataGrid.Columns[i].Visibility == System.Windows.Visibility.Visible)//只导出可见列  
            //    {
            //        dt.Columns.Add(dataGrid.Columns[i].Header.ToString());//构建表头  
            //    }
            //}

            //for (int i = 0; i < dataGrid.Items.Count; i++)
            //{
            //    int columnsIndex = 0;
            //    System.Data.DataRow row = dt.NewRow();
            //    for (int j = 0; j < dataGrid.Columns.Count; j++)
            //    {
            //        if (dataGrid.Columns[j].Visibility == System.Windows.Visibility.Visible)
            //        {
            //            if (dataGrid.Items[i] != null && (dataGrid.Columns[j].GetCellContent(dataGrid.Items[i]) as TextBlock) != null)//填充可见列数据  
            //            {
            //                row[columnsIndex] = (dataGrid.Columns[j].GetCellContent(dataGrid.Items[i]) as TextBlock).Text.ToString();
            //            }
            //            else row[columnsIndex] = "";

            //            columnsIndex++;
            //        }
            //    }
            //    dt.Rows.Add(row);
            //}
            DataTable dt = new DataTable();
            for (int i = 0; i < 6; i++)
            {
                if (i == 0)
                {
                    dt.Columns.Add("课程安排");
                }
                else if (i == 1)
                {
                    dt.Columns.Add("星期一");
                }
                else if (i == 2)
                {
                    dt.Columns.Add("星期二");
                }
                else if (i == 3)
                {
                    dt.Columns.Add("星期三");
                }
                else if (i == 4)
                {
                    dt.Columns.Add("星期四");
                }
                else if (i == 5)
                {
                    dt.Columns.Add("星期五");
                }
            }
            object[] values = new object[5];
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    if (j == 0)
                    {
                        values[j] = _list[i].lesson1;
                    }
                    else if (j == 1)
                    {
                        values[j] = _list[i].lesson2;
                    }
                    else if (j == 2)
                    {
                        values[j] = _list[i].lesson3;
                    }
                    else if (j == 3)
                    {
                        values[j] = _list[i].lesson4;
                    }
                    else if (j == 4)
                    {
                        values[j] = _list[i].lesson5;
                    }
                }
                dt.Rows.Add(values);
                for (int j = 0; j < 5; j++)
                {
                    if (j == 0)
                    {
                        values[j] = _list[i].week1;
                    }
                    else if (j == 1)
                    {
                        values[j] = _list[i].week2;
                    }
                    else if (j == 2)
                    {
                        values[j] = _list[i].week3;
                    }
                    else if (j == 3)
                    {
                        values[j] = _list[i].week4;
                    }
                    else if (j == 4)
                    {
                        values[j] = _list[i].week5;
                    }
                }
                dt.Rows.Add(values);
                for (int j = 0; j < 5; j++)
                {
                    if (j == 0)
                    {
                        values[j] = _list[i].didian1;
                    }
                    else if (j == 1)
                    {
                        values[j] = _list[i].didian2;
                    }
                    else if (j == 2)
                    {
                        values[j] = _list[i].didian3;
                    }
                    else if (j == 3)
                    {
                        values[j] = _list[i].didian4;
                    }
                    else if (j == 4)
                    {
                        values[j] = _list[i].didian5;
                    }
                }
                dt.Rows.Add(values);
                for (int j = 0; j < 5; j++)
                {
                    if (j == 0)
                    {
                        values[j] = _list[i].teacher1;
                    }
                    else if (j == 1)
                    {
                        values[j] = _list[i].teacher2;
                    }
                    else if (j == 2)
                    {
                        values[j] = _list[i].teacher3;
                    }
                    else if (j == 3)
                    {
                        values[j] = _list[i].teacher4;
                    }
                    else if (j == 4)
                    {
                        values[j] = _list[i].teacher5;
                    }
                }
                dt.Rows.Add(values);
            }
            string FileName = ExcelExport(dt, excelTitle);
        }

        public string ExcelExport(DataTable DT, string title)
        {
            try
            {
                //创建Excel
                Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook ExcelBook = ExcelApp.Workbooks.Add(System.Type.Missing);
                //创建工作表（即Excel里的子表sheet） 1表示在子表sheet1里进行数据导出
                Microsoft.Office.Interop.Excel.Worksheet ExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)ExcelBook.Worksheets[1];

                //如果数据中存在数字类型 可以让它变文本格式显示
                ExcelSheet.Cells.NumberFormat = "@";

                //设置工作表名
                ExcelSheet.Name = title;

                //设置Sheet标题
                string start = "A1";
                string end = ChangeASC(DT.Columns.Count) + "1";

                Excel.Range _Range = ExcelSheet.get_Range(start, "G1");
                _Range.Merge(0);    //单元格合并动作(要配合上面的get_Range()进行设计)
                _Range = (Microsoft.Office.Interop.Excel.Range)ExcelSheet.get_Range(start, end);
                _Range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                _Range.Font.Size = 18; //设置字体大小
                _Range.Font.Name = "黑体"; //设置字体的种类 
                ExcelSheet.Cells[1, 1] = title;    //Excel单元格赋值
                _Range.EntireColumn.AutoFit(); //自动调整列宽

                Excel.Range r1 = ExcelSheet.get_Range("A3", "A6");
                r1.Merge(0);
                r1.RowHeight = 26;
                r1.ColumnWidth = 12;
                Excel.Range r2 = ExcelSheet.get_Range("A7", "A10");
                r2.Merge(0);
                r2.RowHeight = 26;
                Excel.Range r3 = ExcelSheet.get_Range("A11", "A14");
                r3.Merge(0);
                r3.RowHeight = 26;
                Excel.Range r4 = ExcelSheet.get_Range("A15", "A18");
                r4.Merge(0);
                r4.RowHeight = 26;
                Excel.Range r5 = ExcelSheet.get_Range("A19", "A22");
                r5.Merge(0);
                r5.RowHeight = 26;
                ExcelSheet.Cells[3, 1] = "一节\n8:20-9:05\n 二节\n9:15-10:00";
                ExcelSheet.Cells[7, 1] = "三节\n10:20-11:05\n四节\n11:15-12:00";
                ExcelSheet.Cells[11, 1] = "五节\n13:30-14:15\n六节\n14:25-15:10";
                ExcelSheet.Cells[15, 1] = "七节\n15:30-16:15\n八节\n16:25-17:00";
                ExcelSheet.Cells[19, 1] = "九节\n18:20-19:05\n十节\n19:15-20:00";
                ExcelSheet.Cells[3, 2] = "课程";
                ExcelSheet.Cells[4, 2] = "教学周";
                ExcelSheet.Cells[5, 2] = "地点";
                ExcelSheet.Cells[6, 2] = "教师";
                ExcelSheet.Cells[7, 2] = "课程";
                ExcelSheet.Cells[8, 2] = "教学周";
                ExcelSheet.Cells[9, 2] = "地点";
                ExcelSheet.Cells[10, 2] = "教师";
                ExcelSheet.Cells[11, 2] = "课程";
                ExcelSheet.Cells[12, 2] = "教学周";
                ExcelSheet.Cells[13, 2] = "地点";
                ExcelSheet.Cells[14, 2] = "教师";
                ExcelSheet.Cells[15, 2] = "课程";
                ExcelSheet.Cells[16, 2] = "教学周";
                ExcelSheet.Cells[17, 2] = "地点";
                ExcelSheet.Cells[18, 2] = "教师";
                ExcelSheet.Cells[19, 2] = "课程";
                ExcelSheet.Cells[20, 2] = "教学周";
                ExcelSheet.Cells[21, 2] = "地点";
                ExcelSheet.Cells[22, 2] = "教师";

                //写表头
                for (int m = 0; m < DT.Columns.Count; m++)
                {
                    ExcelSheet.Cells[2, m + 2] = DT.Columns[m].ColumnName.ToString();

                    start = "B2";
                    end = ChangeASC(DT.Columns.Count) + "2";

                    _Range = ExcelSheet.get_Range(start, "G2");
                    _Range.Font.Size = 13; //设置字体大小
                    _Range.Font.Name = "黑体"; //设置字体的种类  
                    _Range.ColumnWidth = 20; //自动调整列宽 
                    _Range.RowHeight = 18;
                    _Range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                }

                //写数据
                for (int i = 0; i < DT.Rows.Count; i++)
                {
                    for (int j = 0; j < DT.Columns.Count; j++)
                    {
                        //Excel单元格第一个从索引1开始
                        // if (j == 0) j = 1;
                        ExcelSheet.Cells[i + 3, j + 3] = DT.Rows[i][j].ToString();
                    }
                }

                //表格属性设置
                for (int n = 0; n < DT.Rows.Count + 1; n++)
                {
                    start = "A" + (n + 3).ToString();
                    end = ChangeASC(DT.Columns.Count) + (n + 3).ToString();

                    //获取Excel多个单元格区域
                    _Range = ExcelSheet.get_Range(start, end);

                    _Range.Font.Size = 12; //设置字体大小
                    _Range.Font.Name = "宋体"; //设置字体的种类
                    _Range.WrapText = true;

                    //_Range.EntireColumn.AutoFit(); //自动调整列宽
                    _Range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //设置字体在单元格内的对其方式 _Range.EntireColumn.AutoFit(); //自动调整列宽 
                }

                for (int i = 0; i < 2; i++)
                {
                    //获取Excel多个单元格区域
                    _Range = ExcelSheet.get_Range("A1", "G22");
                    _Range.Borders.LineStyle = 1;
                }

                ExcelApp.DisplayAlerts = false; //保存Excel的时候，不弹出是否保存的窗口直接进行保存 

                //弹出保存对话框,并保存文件
                Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
                sfd.FileName = title;
                sfd.DefaultExt = ".xlsx";
                sfd.Filter = "Office 2010 File|*.xlsx|Office 2010 File|*.xls|所有文件|*.*";
                if (sfd.ShowDialog() == true)
                {
                    if (sfd.FileName != "")
                    {
                        ExcelBook.SaveAs(sfd.FileName);  //将其进行保存到指定的路径
                        MessageBox.Show("导出文件存储为: " + sfd.FileName);
                    }
                }

                //释放可能还没释放的进程
                ExcelBook.Close();
                ExcelApp.Quit();
                //PubHelper.Instance.KillAllExcel(ExcelApp);

                return sfd.FileName;
            }
            catch
            {
                MessageBox.Show("导出文件保存失败！");
                return null;
            }
        }

        /// <summary>
        /// 获取当前列列名,并得到EXCEL中对应的列
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private string ChangeASC(int count)
        {
            string ascstr = "";

            switch (count)
            {
                case 1:
                    ascstr = "A";
                    break;
                case 2:
                    ascstr = "B";
                    break;
                case 3:
                    ascstr = "C";
                    break;
                case 4:
                    ascstr = "D";
                    break;
                case 5:
                    ascstr = "E";
                    break;
                case 6:
                    ascstr = "F";
                    break;
                case 7:
                    ascstr = "G";
                    break;
                case 8:
                    ascstr = "H";
                    break;
                case 9:
                    ascstr = "I";
                    break;
                case 10:
                    ascstr = "J";
                    break;
                case 11:
                    ascstr = "K";
                    break;
                case 12:
                    ascstr = "L";
                    break;
                case 13:
                    ascstr = "M";
                    break;
                case 14:
                    ascstr = "N";
                    break;
                case 15:
                    ascstr = "O";
                    break;
                case 16:
                    ascstr = "P";
                    break;
                case 17:
                    ascstr = "Q";
                    break;
                case 18:
                    ascstr = "R";
                    break;
                case 19:
                    ascstr = "S";
                    break;
                case 20:
                    ascstr = "Y";
                    break;
                default:
                    ascstr = "U";
                    break;
            }
            return ascstr;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            var classId = LbClassInfo.SelectedItem as ClassInfo;
            Export(DgCurriculum, classId.Major.ToString() + classId.Class.ToString() + "班");
        }
    }

    public static class ToDataTable
    {
        /// <summary>
        /// Converts a List to a datatable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static DataTable toDataTable<Lesson>(this IList<Lesson> data)
        {
            System.ComponentModel.PropertyDescriptorCollection properties = System.ComponentModel.TypeDescriptor.GetProperties(typeof(Lesson));
            DataTable dt = new DataTable();
            for (int i = 0; i < 6; i++)
            {
                //System.ComponentModel.PropertyDescriptor property = properties[i];
                //dt.Columns.Add(property.Name, property.PropertyType);
                if (i == 0)
                {
                    dt.Columns.Add("课程安排");
                }
                else if (i == 1)
                {
                    dt.Columns.Add("星期一");
                }
                else if (i == 2)
                {
                    dt.Columns.Add("星期二");
                }
                else if (i == 3)
                {
                    dt.Columns.Add("星期三");
                }
                else if (i == 4)
                {
                    dt.Columns.Add("星期四");
                }
                else if (i == 5)
                {
                    dt.Columns.Add("星期五");
                }
            }
            object[] values = new object[properties.Count];
            //foreach (T item in data)
            //{
            //    for (int i = 0; i < 5; i++)
            //    {
            //        values[i] = properties[i].GetValue(item);
            //    }
            //    dt.Rows.Add(values);
            //}

            //for (int i = 0; i < 5; i++)
            //{
            //    values[i] = data[i].week1;
            //    values[i] = data[i].week2;
            //    values[i] = data[i].week3;
            //    values[i] = data[i].week4;
            //    values[i] = data[i].week5;
            //    dt.Rows.Add(values);
            //}
            return dt;
        }
    }
}